package com.module.product.manager;

import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.Transaction;

import base.common.CUDEnum;
import base.dao.GenericHibernateDao;
import base.db.HibernateSessionFactory;
import base.manager.GenericHibernateManager;
import base.util.Page;
import base.util.StringUtil;
import base.util.Tools;
import base.util.transaction.TransactionCallback;
import base.util.transaction.TransactionTemplate;

import com.module.product.common.IDManage;
import com.module.product.common.ProductConstant;
import com.module.product.dao.AttrItemComboxDao;
import com.module.product.dao.AttrItemDao;
import com.module.product.dao.AttrValueDao;
import com.module.product.dao.AttrValueTemporaryDao;
import com.module.product.dao.AttrValueTemporarySynDao;
import com.module.product.dao.CategoryDao;
import com.module.product.dao.CategoryProductDao;
import com.module.product.dao.EntAttrValueDao;
import com.module.product.dao.EntAttrValueSynDao;
import com.module.product.dao.EntProductDao;
import com.module.product.dao.EntProductSynDao;
import com.module.product.dao.ProductDao;
import com.module.product.dao.ProductTemporaryDao;
import com.module.product.dao.ProductTemporarySynDao;
import com.module.product.vo.AttrItem;
import com.module.product.vo.AttrValueTemporary;
import com.module.product.vo.AttrValueTemporarySyn;
import com.module.product.vo.Category;
import com.module.product.vo.EntAttrValue;
import com.module.product.vo.EntAttrValueSyn;
import com.module.product.vo.EntProduct;
import com.module.product.vo.EntProductSyn;
import com.module.product.vo.Product;
import com.module.product.vo.ProductTemporary;
import com.module.product.vo.ProductTemporarySyn;
import com.util.PropertiesUtil;

/**
 * 商品业务处理类
 * 
 * @author 王勇云
 * @version [V1.0, Jul 22, 2011]
 * @see [相关类/方法]
 * @since [二代服务平台/商品]
 */
public class EntProductManager extends GenericHibernateManager<EntProduct,String>{
	EntProductDao dao = EntProductDao.getInstance();
	private String downloadPath = PropertiesUtil.getString("downloadPath");
	private String downloadHostName = PropertiesUtil.getString("downloadHostName");
	private int downloadPort = Integer.parseInt(PropertiesUtil.getString("downloadPort"));
	AttrValueDao attrValueDao = AttrValueDao.getInstance();
	AttrItemComboxDao comboxDao = AttrItemComboxDao.getInstance();
	ProductDao productDao = ProductDao.getInstance();   
	
	EntAttrValueDao entAttrValueDao = EntAttrValueDao.getInstance();
	AttrItemDao attrItemDao = AttrItemDao.getInstance();
	CategoryProductDao categoryProductDao = CategoryProductDao.getInstance();
	ProductTemporaryDao productTemporaryDao = ProductTemporaryDao.getInstance();
	ProductTemporarySynDao productTemporarySynDao = ProductTemporarySynDao.getInstance();
	EntProductSynDao entProductSynDao = EntProductSynDao.getInstance();
	AttrValueTemporaryDao attrValueTemporaryDao = AttrValueTemporaryDao.getInstance();
	AttrValueTemporarySynDao attrValueTemporarySynDao = AttrValueTemporarySynDao.getInstance();
	EntAttrValueSynDao entAttrValueSynDao = EntAttrValueSynDao.getInstance();
    CategoryDao categoryDao = CategoryDao.getInstance();
	
	private static EntProductManager instance = new EntProductManager();

	static Logger logger = Logger.getLogger(EntProductManager.class.getName());
	private EntProductManager() {
	}

	public synchronized static EntProductManager getInstance() {
		return instance;
	}
	
	public Map<String,Object> saveProductAndAttr(final EntProduct obj) {
		final Session session = HibernateSessionFactory.getSession();
		final Connection conn = session.connection();
		TransactionTemplate template = new TransactionTemplate(session);
		template.getJsonMsg().setMsg("\u4fdd\u5b58\u6210\u529f");//保存成功
//		final EntProduct obj = unit.getEntProduct();
		
			template.execute(new TransactionCallback() {
				@Override
				public void doInTransaction() {
					if (StringUtils.isBlank(obj.getId())) {// 添加
						obj.setId(IDManage.getUUIDString());
						obj.setStatus(CUDEnum.C.toString());
						obj.setCreateTime(new Date());
						dao.save(session,obj);
					} else {
						obj.setStatus(CUDEnum.U.toString());
						obj.setUpdateTime(new Date());
						dao.checkProductState(conn,obj.getId()); 
						dao.update(session,obj);
					}
					
					
					//新增同步产品临时
					EntProductSyn eps = new EntProductSyn();	
					try {
						PropertyUtils.copyProperties(eps, obj);
					} catch (IllegalAccessException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (InvocationTargetException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (NoSuchMethodException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					eps.setSyscStatus(obj.getStatus());
					eps.setSyscTime(new Date());
					eps.setIssysc(ProductConstant.NOTSYSC);
					entProductSynDao.save(session, eps);
					
					categoryProductDao.deleteAllCategorybyProductId(conn,obj.getId());
					List<String> categorylist = obj.getCategoryList();
					if (categorylist != null) {
						for (String cateId : categorylist) {
							categoryProductDao.saveCategoryIdandProductId(conn,cateId,
									obj.getId());
						}
					}
					saveAtt(session,conn,obj.getId(),obj.getApproveState(), obj.getAttrValueList());
					if (obj.getApproveState().intValue() == ProductConstant.APPROVE_WAIT_APPROVE
							.intValue()) {
						ProductTemporary pt = new ProductTemporary();
						try {
							PropertyUtils.copyProperties(pt, obj);
						} catch (IllegalAccessException e) {
							e.printStackTrace();
						} catch (InvocationTargetException e) {
							e.printStackTrace();
						} catch (NoSuchMethodException e) {
							e.printStackTrace();
						}
						pt.setEntProductId(obj.getId());
						productTemporaryDao.save(session,pt);
						//新增同步产品临时
						ProductTemporarySyn pts = new ProductTemporarySyn();	
						try {
							PropertyUtils.copyProperties(pts, pt);
						} catch (IllegalAccessException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						} catch (InvocationTargetException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						} catch (NoSuchMethodException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
						pts.setSyscStatus(CUDEnum.C.toString());
						pts.setSyscTime(new Date());
						pts.setIssysc(ProductConstant.NOTSYSC);
						productTemporarySynDao.save(session, pts);
						
						categorylist = pt.getCategoryList();
						if (categorylist != null) {
							for (String cateId : categorylist) {
								categoryProductDao.saveCategoryIdandProductId(conn,cateId,
										pt.getId());
							}
						}
						List<EntAttrValue> list = entAttrValueDao.getEntAttvByProductid(session,obj
								.getId());
						for (EntAttrValue eav : list) {
							AttrValueTemporary avt = new AttrValueTemporary();
							try {
								PropertyUtils.copyProperties(avt, eav);
								avt.setProductId(pt.getId());
							} catch (IllegalAccessException e) {
								e.printStackTrace();
							} catch (InvocationTargetException e) {
								e.printStackTrace();
							} catch (NoSuchMethodException e) {
								e.printStackTrace();
							}
							attrValueTemporaryDao.save(session,avt);
							//插入同步数据
							AttrValueTemporarySyn avts = new AttrValueTemporarySyn();
							try {
								PropertyUtils.copyProperties(avts,avt);
								avts.setSyscStatus(CUDEnum.C.toString());
								avts.setSyscTime(new Date());
								avts.setIssysc(ProductConstant.NOTSYSC);
								avt.setProductId(pt.getId());
							} catch (IllegalAccessException e) {
								e.printStackTrace();
							} catch (InvocationTargetException e) {
								e.printStackTrace();
							} catch (NoSuchMethodException e) {
								e.printStackTrace();
							}
							attrValueTemporarySynDao.save(session,avts);
							
						}
					}
				}
			});
			return template.getJsonMsg().getMap();
	}

	
	/**
	 * 提交审核
	 * @param productList
	 * @throws Exception
	 * @author dengsq
	 */
	@SuppressWarnings("deprecation")
	public Map submitAudit(final List<EntProduct> productList) throws Exception{
		final Session session = HibernateSessionFactory.getSession();
		TransactionTemplate template = new TransactionTemplate(session);
		final Connection conn = session.connection();
		template.getJsonMsg().setMsg(ProductConstant.OPT_SUESS);
			template.execute(new TransactionCallback() {
			@SuppressWarnings("unchecked")
			@Override
			public void doInTransaction() {
				//List<EntProduct> productList = beanUnit.getProductList();
				if(productList != null && !productList.isEmpty()){
					for(EntProduct obj : productList){
						ProductTemporary pt = new ProductTemporary();	
						
						try {
							PropertyUtils.copyProperties(pt, obj);
							
						} catch (IllegalAccessException e) {
							e.printStackTrace();
						} catch (InvocationTargetException e) {
							e.printStackTrace();
						} catch (NoSuchMethodException e) {
							e.printStackTrace();
						}
						dao.update(session, obj);
						//同步产品
						EntProductSyn eps = new EntProductSyn();	
						try {
							PropertyUtils.copyProperties(eps, obj);
						} catch (IllegalAccessException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						} catch (InvocationTargetException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						} catch (NoSuchMethodException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
						eps.setSyscStatus(CUDEnum.U.toString());
						eps.setSyscTime(new Date());
						eps.setIssysc(ProductConstant.NOTSYSC);
						entProductSynDao.save(session, eps);
						
						pt.setEntProductId(obj.getId());
						productTemporaryDao.save(session,pt);
						//新增同步产品临时
						ProductTemporarySyn pts = new ProductTemporarySyn();	
						try {
							PropertyUtils.copyProperties(pts, pt);
						} catch (IllegalAccessException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						} catch (InvocationTargetException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						} catch (NoSuchMethodException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
						pts.setSyscStatus(CUDEnum.C.toString());
						pts.setSyscTime(new Date());
						pts.setIssysc(ProductConstant.NOTSYSC);
						productTemporarySynDao.save(session, pts);
						
						List<String> categorylist = pt.getCategoryList();
						if (categorylist != null) {
							for (String cateId : categorylist) {
								categoryProductDao.saveCategoryIdandProductId(conn,cateId,
										pt.getId());
							}
						}
						List<EntAttrValue> list = entAttrValueDao.getEntAttvByProductid(session,obj
								.getId());
						for (EntAttrValue eav : list) {
							AttrValueTemporary avt = new AttrValueTemporary();
							try {
								PropertyUtils.copyProperties(avt, eav);
							} catch (IllegalAccessException e) {
								e.printStackTrace();
							} catch (InvocationTargetException e) {
								e.printStackTrace();
							} catch (NoSuchMethodException e) {
								e.printStackTrace();
							}
							avt.setProductId(pt.getId());
							attrValueTemporaryDao.save(session,avt);
							//插入同步数据
							AttrValueTemporarySyn avts = new AttrValueTemporarySyn();
							try {
								PropertyUtils.copyProperties(avts,avt);
								avts.setSyscStatus(CUDEnum.C.toString());
								avts.setSyscTime(new Date());
								avts.setIssysc(ProductConstant.NOTSYSC);
								avt.setProductId(pt.getId());
							} catch (IllegalAccessException e) {
								e.printStackTrace();
							} catch (InvocationTargetException e) {
								e.printStackTrace();
							} catch (NoSuchMethodException e) {
								e.printStackTrace();
							}
							attrValueTemporarySynDao.save(session,avts);
						}
					}
				}				
			}
		});
		return template.getJsonMsg().getMap();
	}
	
	/**
	 * 导入商品
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	public boolean importProduct(List<EntProduct> list) throws Exception{
		Session session =this.getSession();
	    Transaction tx =session.beginTransaction();
	    boolean flag = false;	   
	    Connection conn = session.connection();
	    try{
		    if(list != null && !list.isEmpty()){
		    	for(EntProduct obj:list){
		    		obj.setId(IDManage.getUUIDString());
					obj.setStatus(CUDEnum.C.toString());
					obj.setCreateTime(new Date());
					dao.save(this.getSession(),obj);
					
					List<String> categorylist = obj.getCategoryList();
					if (categorylist != null) {
						for (String cateName : categorylist) {
							String id = categoryDao.getCategoryIdByName(session,cateName);
							if(id == null){
								return false;
							}
							categoryProductDao.saveCategoryIdandProductId(conn,id,
									obj.getId());
						}
					}
		    	}
		    } 	
			tx.commit();
			flag = true;
	    }catch (Exception e) {
			tx.rollback();
			flag = false;
			throw e;
		}finally{
			this.closeSession();
		}
		return flag;
	}
	
	private boolean saveAtt(Session session,Connection conn,String productId,Integer approveState, List listAtt) {
		List<Map<String,Object>> list = listAtt;
		List<EntAttrValue> attrList = new ArrayList<EntAttrValue>();
		Set<String> groupIdSet = new HashSet<String>();
		EntAttrValue attrValue = new EntAttrValue();
		boolean saveret = false;
		if (list != null) {
			for (Map<String,Object> a : list) {
				String name = null;
				String value = null;
				String id = null;
				String attrGroup = null;
				Integer attrValueSize = null;
				String originalValue = null;
				if (Tools.isValid(a.get("name"))) {
					name = (String) a.get("name");
				}
				if (Tools.isValid(a.get("attrValue"))) {
					value = (String) a.get("attrValue");
				}
				if (Tools.isValid(a.get("id"))) {
					id = (String) a.get("id");
				}
				if (Tools.isValid(a.get("attrGroup"))) {
					attrGroup = (String) a.get("attrGroup");
				}
				if (Tools.isValid(a.get("attrValueSize"))) {
					attrValueSize = (Integer) a.get("attrValueSize") ;
				}
				if (Tools.isValid(a.get("originalValue"))) {
					originalValue = (String) a.get("originalValue");
				}
				attrValue = new EntAttrValue(IDManage.getUUIDString(), name,
						value, id, productId, attrGroup, attrValueSize,originalValue);
				attrList.add(attrValue);
				groupIdSet.add(attrGroup);
			}
		}
		
		  //同步表新增产品属性值同步数据
		 List<EntAttrValue> attrValueList = entAttrValueDao.getEntAttvByProductid(session,productId);
   		 for(EntAttrValue v:attrValueList){
 		    	EntAttrValueSyn avSyn = new EntAttrValueSyn();
 		    	avSyn.setSyscStatus(CUDEnum.D.toString());
 		    	avSyn.setProductId(productId);
 		    	avSyn.setId(v.getId());
 		    	avSyn.setSyscTime(new Date());
 		    	avSyn.setIssysc(ProductConstant.NOTSYSC);
 		    	entAttrValueSynDao.save(session,avSyn);
   		 }
			entAttrValueDao.delAllAttrVbyPid(conn,productId);
		for(EntAttrValue v:attrList){
			saveret = entAttrValueDao.save(session,v);
			
			//插入同步数据
			EntAttrValueSyn avSyn = new EntAttrValueSyn();
			
			try {
				PropertyUtils.copyProperties(avSyn, v);
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    	avSyn.setSyscStatus(CUDEnum.C.toString());
	    	avSyn.setSyscTime(new Date());
	    	avSyn.setIssysc(ProductConstant.NOTSYSC);
	    	entAttrValueSynDao.save(session,avSyn);
		}
		return saveret;
	}

	public EntProduct getEntProduct(String id) throws Exception {
		Session session = HibernateSessionFactory.getSession();
    	Connection conn = session.connection();
		EntProduct p = dao.get(session,id);
    	try{
    		p.setCategoryList(categoryProductDao.findCategoryIdbyProductId(conn,id));
    		
    	}catch(Exception e){
    		throw e;
    		
    	}finally{
    		this.close(conn);
    		this.closeSession();
    	}
		return p;
	}

	public Page getList(EntProduct obj, Page page) throws Exception {
		Session session = HibernateSessionFactory.getSession();		
		Connection conn = session.connection();
		Page p=null;
		try{
			p = dao.getList(session,obj, page);
			setCategoryName(session,conn,p);
    	}catch(Exception e){
    		throw e;
    		
    	}finally{
    		this.close(conn);
    		this.closeSession();
    	}
		return p;
	}

	private void setCategoryName(Session session,Connection conn,Page page) throws Exception {
		try{
			List list = page.getRows();
			Category c = null;
			if (list != null) {
				for (EntProduct p : (List<EntProduct>) list) {
					List<Category> cList = categoryProductDao.findCategoryListbyProductId(conn,p.getId());
					p.setCategoryName(getCategoryNamesFromList(cList));
					p.setCategoryList(this.getCategoryIdsFormList(cList));					
				}
			}			
    	}catch(Exception e){
    		throw e;
    		
    	}
	}

	private String getCategoryNamesFromList(List<Category> list) {
		StringBuffer categoryNames = new StringBuffer();
		if (list != null) {
			for (int i = 0; i < list.size(); i++) {
				if (i != 0) {
					categoryNames.append(" , ");
				}
				categoryNames.append(list.get(i).getName());
			}
		}
		return categoryNames.toString();
	}
	
	private List<String> getCategoryIdsFormList(List<Category> list){
		List<String> categoryIds = new ArrayList<String>();
		if (list != null) {
			for (int i = 0; i < list.size(); i++) {
				categoryIds.add(list.get(i).getId());
			}
		}
		return categoryIds;
	}

	public List<Product> findProductListbyOrgId(EntProduct obj) throws Exception {
		Session session =HibernateSessionFactory.getSession();
		Transaction tx =session.beginTransaction();
        List<Product> list = null;
		try{
		    	list = dao.findProductListbyOrgId(session,obj);
		    	tx.commit();
	    	}catch(Exception e){
	    		tx.rollback();
	    		throw e;
	    		
	    	}finally{
	    		this.closeSession();
	    	}
		return list;
	}

	public EntProduct getremotProduct(String id) {
		// Product p = dao.get(id);
		// p.setList(attrValueDao.getattvbyproductid(p.getId()));
		return null;
	}
	/*
	public ProductBeanUnit decreaseProductStocks(ProductBeanUnit beanUnit) throws Exception {
		Session session =HibernateSessionFactory.getSession();
		Transaction tx =session.beginTransaction();
		boolean b = false;
		ProductBeanUnit util = null;
		try{
				b = dao.decreaseProductStocks(session,beanUnit.getList());
				Map map = new HashMap();
				map.put("state", b);
				if (b) {
					map.put("msg", "保存成功");
				} else {
					map.put("msg", "保存失败");
				}
				util = ProductUtil.createNewBean(beanUnit);
				util.setResult(map);
				tx.commit();
	    	}catch(Exception e){
	    		tx.rollback();
	    		throw e;
	    		
	    	}finally{
	    		this.closeSession();
	    	}
			return util;
	}
	*/
	/**
	 * 审核
	 * @param productObj
	 * @throws Exception
	 * @author huh
	 */
	public Map Audit(final EntProduct obj){
		final Session session = HibernateSessionFactory.getSession();
		final Connection conn = session.connection();
		TransactionTemplate template = new TransactionTemplate(session);
		  final int state=obj.getApproveState();//审核状态
		  if(state == 3){
			  template.getJsonMsg().setMsg("审核成功!");
		  }else{
			  template.getJsonMsg().setMsg("审核不通过!");
		  }
		try{
			template.execute(new TransactionCallback() {
				@Override
				public void doInTransaction() {
					dao.update(session, obj); 
				}}
				);
				
			
		}catch(Exception e)
		{
			
			template.getJsonMsg().setMsg("审核失败");
			e.printStackTrace();
		}
			
		return template.getJsonMsg().getMap();
	}
	public Map<String,Object> delete(final EntProduct obj,String str) {
		//final EntProduct obj = beanUnit.getEntProduct();
		final Session session =HibernateSessionFactory.getSession();
		final TransactionTemplate template = new TransactionTemplate(session);
		template.getJsonMsg().setMsg("\u5220\u9664\u6210\u529f");//删除成功
		

			template.execute(new TransactionCallback() {
				@Override
				public void doInTransaction() {
			        template.setJsonMsg(dao.deleteObjs(session,obj));
				}});  
		return template.getJsonMsg().getMap();
	}
	
	@SuppressWarnings("deprecation")
	public List<AttrItem> listByGroupId(AttrItem obj) throws Exception {
		Session session =HibernateSessionFactory.getSession();
		Connection conn = session.connection();
		List<AttrItem> attrList  = new ArrayList<AttrItem>();
		try{
			List<AttrItem> list = entAttrValueDao.listByGroupId(conn,obj.getAttrGroup(),obj.getProductId());
			attrList = this.getComboxVaule(list, session);
	    	}catch(Exception e){
	    		throw e;    		
	    	}finally{
	    		this.close(conn);
	    		this.closeSession();
	    	}
		return attrList;
	}
	
	private List<AttrItem> getComboxVaule(List<AttrItem> list,Session session) throws Exception{
		List<AttrItem> attrList  = new ArrayList<AttrItem>();	
		if(list != null && ! list.isEmpty()){
			for(AttrItem atrrItem : list){
				List<String> comboxList = comboxDao.getComboxByAtrrId(session, atrrItem.getId());
				atrrItem.setComboxList(comboxList);
				attrList.add(atrrItem);
				if(this.isImgOrVideo(atrrItem)){
					atrrItem.setNewAttrValue(atrrItem.getAttrValue());
					if(!StringUtil.isNull(atrrItem.getAttrValue())){
						atrrItem.setAttrValue(ProductConstant.HTTP_MEET+downloadHostName+":"+downloadPort+downloadPath + atrrItem.getAttrValue());
					}				
				}
			}
		}		
		return  attrList;
	}
	
	/**
	 * 是否是图片或者视频
	 * @param item
	 * @return
	 */
	private boolean isImgOrVideo(AttrItem item){
		if(item != null && item.getType() != null
				&&(item.getType().equals(ProductConstant.ATTR_TYPE_PICTURE) 
		    			|| item.getType().equals(ProductConstant.ATTR_TYPE_VEDIO)))
			return true;
		return false;
	}
	
	
	public EntProduct findByName(EntProduct bean) throws Exception{
		Session session =HibernateSessionFactory.getSession();
		 EntProduct ent=null;
		String productname = bean.getName();
		String hql = "from EntProduct as obj where name='"+productname+"' and status <> 'D'";

		try{
			  	ent = dao.findUniqueObjectBySQL(session,hql);
		    }catch(Exception e){
		    	throw e;
		    	
		    }finally{
		    	this.closeSession();
		    }

		return ent;
		
	}
	
	public List<AttrItem> listByCategoryId(AttrItem obj) throws Exception{
    	Session session =HibernateSessionFactory.getSession();
    	Connection conn = session.connection();
    	List<AttrItem> list  = new ArrayList();
    	try{
    		List<AttrItem> atrtlist  = dao.listByCategoryId(conn,obj.getAttrGroup(),obj.getProductId());
    		list = this.getComboxVaule(atrtlist, session);
    	}catch(Exception e){
    		throw e;
    		
    	}finally{
    		this.close(conn);
    		this.closeSession();
    	}
        return list;
    }
	//审核商品
	/*
    public void audit(ProductBeanUnit unit){
    	final Session session = HibernateSessionFactory.getSession();
    	final Connection conn = session.connection();
		TransactionTemplate template = new TransactionTemplate(session);
		template.getJsonMsg().setMsg("\u5ba1\u6279\u6210\u529f");//审批成功
		final EntProduct obj = unit.getEntProduct();
        final int state=obj.getApproveState();//审核状态
        	 template.execute(new TransactionCallback() {
     			@Override
     			public void doInTransaction() {
     				Product product = productDao.get(session,obj.getId());
     		        if(product==null){//说明正式库中不存在
     		        	if(state==3){
     		        		try {
     		        			obj.setApproveTime(new Date());
     		        		    dao.update(session,obj);
     		        		    
     		        		   ProductTemporary productTemporary=productTemporaryDao.getProductTemporary(session,obj.getId());
     		        		    
     		        		   productTemporary.setApproveState(ProductConstant.APPROVE_STATE_THROUGH_APPROVE);
     		        		   productTemporary.setApproveTime(new Date());
     		        		   productTemporary.setApproveComment(obj.getApproveComment());
     		        		    
     		        		  productTemporaryDao.update(session,productTemporary);  
     		        		    
     		        		    product = new Product();
     		        		    PropertyUtils.copyProperties(product, obj);
     		        		    
     		        		    productDao.save(session,product);
     		        		    
     		        		    List<EntAttrValue> list=entAttrValueDao.getEntAttvByProductid(session,obj.getId());
     		        		    
     		         		    for(EntAttrValue v:list){
     		         		    	AttrValue av = new AttrValue();
     		         		    	PropertyUtils.copyProperties(av, v);
     		         		    	attrValueDao.save(session,av);
     		         		    }
     		        		    
     						} catch (Exception e) {
     				    		e.printStackTrace();
     						}
     		        	}else if(state==4){//不通过
     		        		obj.setApproveTime(new Date());
     		    		    dao.update(session,obj);
     		    		   ProductTemporary productTemporary=productTemporaryDao.getProductTemporary(session,obj.getId());
		        		    
 		        		   productTemporary.setApproveState(ProductConstant.APPROVE_STATE_THROUGH_APPROVE);
 		        		   productTemporary.setApproveTime(new Date());
 		        		   productTemporary.setApproveComment(obj.getApproveComment());
 		        		    
 		        		  productTemporaryDao.update(session,productTemporary);  
 		        		    
     		    		    
     		        	}
     		        }else{//正式库中存在
     		        	if(state==3){
     		        		obj.setApproveTime(new Date());
     		    		    dao.update(session,obj);
     		    		    try {
     							PropertyUtils.copyProperties(product, obj);
     							productDao.update(session,product);
     						} catch (Exception e) {     							
     							e.printStackTrace();
     						}
     						ProductTemporary productTemporary=productTemporaryDao.getProductTemporary(session,obj.getId());
  		        		    
   		        		   productTemporary.setApproveState(ProductConstant.APPROVE_STATE_THROUGH_APPROVE);
   		        		   productTemporary.setApproveTime(new Date());
   		        		   productTemporary.setApproveComment(obj.getApproveComment());
   		        		    
   		        		  productTemporaryDao.update(session,productTemporary);  
   		        		    
     		     		    
     		     		   attrValueDao.delAllAttrVbyPid(conn,obj.getId());
     		     		   List<EntAttrValue> list=entAttrValueDao.getEntAttvByProductid(session,obj.getId());
     		    		    for(EntAttrValue v:list){
     		    		    	AttrValue av = new AttrValue();
     		    		    	try {
     								PropertyUtils.copyProperties(av, v);
     							} catch (IllegalAccessException e) {
     								e.printStackTrace();
     							} catch (InvocationTargetException e) {
     								e.printStackTrace();
     							} catch (NoSuchMethodException e) {
     								e.printStackTrace();
     							}
     		    		    	attrValueDao.save(session,av);
     		    		    }
     		     		    
     		        	}else{
     		        		obj.setApproveTime(new Date());
     		    		    dao.update(session,obj);
     		    		   ProductTemporary productTemporary=productTemporaryDao.getProductTemporary(session,obj.getId());
		        		    
 		        		   productTemporary.setApproveState(ProductConstant.APPROVE_STATE_THROUGH_APPROVE);
 		        		   productTemporary.setApproveTime(new Date());
 		        		   productTemporary.setApproveComment(obj.getApproveComment());
 		        		    
 		        		  productTemporaryDao.update(session,productTemporary);  
 		        		    
     		        	}
     		        }
     			}
             });
             unit.setResult(template.getJsonMsg().getMap());
    }
    */
	@Override
	public GenericHibernateDao<EntProduct, String> getDao() {
		// TODO Auto-generated method stub
		return dao;
	}
}
